package kr.hkit.PowerJava.chapter16;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.BoxLayout;

import java.awt.event.ContainerListener;
import java.awt.event.ContainerEvent;

import javax.swing.JTextField;

import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;

public class TotalEventEx extends JFrame implements ComponentListener, ActionListener, ContainerListener, FocusListener, WindowListener, WindowStateListener {

	private JPanel contentPane;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JPanel panel;
	private JButton btnNewButton_2;
	private JTextField textField;
    private JLabel lbl;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TotalEventEx frame = new TotalEventEx();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TotalEventEx() {
		initialize();
	}
	private void initialize() {
		addWindowStateListener(this);
		addWindowListener(this);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.addContainerListener(this);
		contentPane.addComponentListener(this);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		btnNewButton = new JButton("New button");
		btnNewButton.addActionListener(this);
		contentPane.add(btnNewButton, BorderLayout.SOUTH);
		
		panel = new JPanel();
		contentPane.add(panel, BorderLayout.EAST);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		btnNewButton_1 = new JButton("컨포넌트추가");
		panel.add(btnNewButton_1);
		
		btnNewButton_2 = new JButton("컨포넌트제거");
		btnNewButton_2.addActionListener(this);
		panel.add(btnNewButton_2);
		btnNewButton_1.addActionListener(this);
		
		textField = new JTextField();
		textField.addFocusListener(this);
		textField.setText("ㅎㅎ");
		textField.setToolTipText("한번 넣어봐");
		contentPane.add(textField, BorderLayout.NORTH);
		textField.setColumns(10);
	}

	public void componentHidden(ComponentEvent e) {
		display("componentHidden", e);
    }
	public void componentMoved(ComponentEvent e) {
		display("componentMoved", e);
	}
	public void componentResized(ComponentEvent e) {
		display("componentResized", e);
	}
	public void componentShown(ComponentEvent e) {
		display("componentShown", e);
	}
	protected void componentHiddenContentPane(ComponentEvent e) {
	}
	public void display(String s,ComponentEvent  e){
		System.out.println(s + e.getComponent());
	}
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnNewButton_2) {
			actionPerformedBtnNewButton_2(e);
		}
		if (e.getSource() == btnNewButton) {
			actionPerformedBtnNewButton(e);
		}
		if (e.getSource() == btnNewButton_1) {
			actionPerformedBtnNewButton_1(e);
		}
	}
	protected void actionPerformedBtnNewButton_1(ActionEvent e) {
	  lbl = new JLabel("추가");
		contentPane.add(lbl, BorderLayout.CENTER);
	  revalidate();
	}
	protected void actionPerformedBtnNewButton(ActionEvent e) {
		setVisible(false);
	}
	protected void actionPerformedBtnNewButton_2(ActionEvent e) {
		contentPane.remove(lbl);
		repaint();
	}
	public void componentAdded(ContainerEvent e) {
		System.out.println("componentAdded");
		System.out.println("getComponent="+e.getComponent()
				+"e.getChild() = " + e.getChild()
				+"e.getContainer() = "+e.getContainer());
	}
	public void componentRemoved(ContainerEvent e) {
		System.out.println("componentRemoved");
		System.out.println("getComponent="+e.getComponent()
				+"e.getChild() = " + e.getChild()
				+"e.getContainer() = "+e.getContainer());
	
	}
	public void focusGained(FocusEvent e) {
	 System.out.println("focusGained");
	}
	public void focusLost(FocusEvent e) {
		System.out.println("focusLost");
	}

	public void windowActivated(WindowEvent e) {
		System.out.println("windowActivated");
	}
	public void windowClosed(WindowEvent e) {
		System.out.println("windowClosed");
	}
	public void windowClosing(WindowEvent e) {
		System.out.println("windowClosing");
	}
	public void windowDeactivated(WindowEvent e) {
		System.out.println("windowDeactivated");
	}
	public void windowDeiconified(WindowEvent e) {
		System.out.println("windowDeiconified");
	}
	public void windowIconified(WindowEvent e) {
		System.out.println("windowIconified");
	}
	public void windowOpened(WindowEvent e) {
		System.out.println("windowOpened");
	}
	public void windowStateChanged(WindowEvent e) {
		System.out.println("windowStateChanged " + e.getNewState() + ":" + e.getOldState());
	}
	}
